﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.Common;
using System.Transactions;
using Apps.Models.Flow;
using Apps.IBLL.Flow;
using Apps.IDAL.Flow;
using Apps.BLL.Core;
using Apps.Locale;
using Apps.Models.Enum;
using System.Linq.Expressions;

namespace Apps.BLL.Flow
{
    public partial class Flow_StepBLL
    {
        public List<V_FLOW_STEPINFOModel> GetStepCheckList(ref GridPager pager, Expression<Func<V_FLOW_STEPINFO, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                var queryData = db.V_FLOW_STEPINFO.Where(whereLambda); 
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                var list = queryData.ToList();
                return TransHelper.TransExpList<V_FLOW_STEPINFO, V_FLOW_STEPINFOModel>(list);
            }
        }

        public override List<Flow_StepModel> GetList(ref GridPager pager, string formId)
        {

            IQueryable<Flow_Step> queryData = null;

            queryData = m_Rep.GetList(a => a.FormId == formId).OrderBy(a=>a.Sort);
        
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public List<Flow_StepModel> GetHisStepBycontentId(string contentId)
        {
            IQueryable<Flow_Step> queryData = null;

            queryData = m_Rep.GetHisStepBycontentId(contentId);

            return CreateModelList(ref queryData);
        }


        public Flow_Step GetNextStepByauditId(string auditId)
        {
            using (DBContainer db = new DBContainer())
            {
                var queryData = db.V_FLOW_STEPINFO.Where(a=>a.ID==auditId).OrderByDescending(a=>a.STEPNO).FirstOrDefault();

                var query = db.Flow_Step.Where(a => a.Sort > queryData.STEPNO);
                if(query.Count()>0)
                {
                    return query.FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
        }

        public Flow_StepModel GetNextStepBycontentId(string curStepId, string contentId)
        {
            var entity = m_Rep.GetNextStepBycontentId(curStepId,contentId);
            if (entity != null)
            {
                Flow_StepModel model = new Flow_StepModel();
                model.Id = entity.Id;
                model.Name = entity.Name;
                model.Remark = entity.Remark;
                model.Sort = entity.Sort;
                model.FormId = entity.FormId;
                model.FlowRule = entity.FlowRule;
                model.IsCustom = entity.IsCustom;
                model.IsAllCheck = entity.IsAllCheck;
                model.Execution = entity.Execution;
                model.CompulsoryOver = entity.CompulsoryOver;
                model.IsEditAttr = entity.IsEditAttr;
                if (!string.IsNullOrEmpty(entity.Execution))
                {
                    DBContainer db = new DBContainer();
                    string[] arr = entity.Execution.Split(',');
                    List<string> executionnames = new List<string>();
                    switch (entity.FlowRule)
                    {
                        case 2:
                            executionnames = db.SysUser.Where(a => arr.Contains(a.Id)).Select(a => a.TrueName).ToList();
                            break;
                        case 4:
                            executionnames = db.SysPosition.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                        case 5:
                            executionnames = db.SysStruct.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                    }
                    model.ExecutionName = string.Join(",", executionnames.ToArray());
                } 
                 
                return model;
            }

            return null;
        }

        public string GetFirstStepUserName(string contentid)
        {
            using (DBContainer db = new DBContainer())
            {
                var query = from r in db.Flow_FormContentStepCheckState
                            join s in db.Flow_FormContentStepCheck
                            on r.StepCheckId equals s.Id 
                            where s.StepId == "00" && s.ContentId == contentid
                            select r.SysUser.TrueName;
                return query.FirstOrDefault();
            } 
        }

        public void UpdateStepNo(string formid)
        {
            using (DBContainer db = new DBContainer())
            {
                var query = from s in db.Flow_Step
                            where s.FormId == formid
                            orderby s.Sort ascending
                            select s;
                int i = 1;
                foreach(var step in query)
                {
                    step.Sort = i;
                    i++;
                }
                db.SaveChanges();
            }
        }

        public Flow_StepModel GetNextStepByCurStepId(string curStepId,string formid)
        { 
            var entity = m_Rep.GetNextStepByCurStepId(curStepId, formid);
            if (entity != null)
            {
                Flow_StepModel model = new Flow_StepModel();
                model.Id = entity.Id;
                model.Name = entity.Name;
                model.Remark = entity.Remark;
                model.Sort = entity.Sort;
                model.FormId = entity.FormId;
                model.FlowRule = entity.FlowRule;
                model.IsCustom = entity.IsCustom;
                model.IsAllCheck = entity.IsAllCheck;
                model.Execution = entity.Execution;
                model.CompulsoryOver = entity.CompulsoryOver;
                model.IsEditAttr = entity.IsEditAttr;
                model.StepNo = string.Format("第{0}步",entity.Sort) ;
                if (!string.IsNullOrEmpty(entity.Execution))
                {
                    DBContainer db = new DBContainer();
                    string[] arr = entity.Execution.Split(',');
                    List<string> executionnames = new List<string>();
                    switch (entity.FlowRule)
                    {
                        case 2:
                            executionnames = db.SysUser.Where(a => arr.Contains(a.Id)).Select(a => a.TrueName).ToList();
                            break;
                        case 4:
                            executionnames = db.SysPosition.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                        case 5:
                            executionnames = db.SysStruct.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                    }
                    model.ExecutionName = string.Join(",", executionnames.ToArray());
                }

                return model;
            }

            return null;
        }

        public override Flow_StepModel GetById(object id)
        {
            if (IsExists(id))
            {
                Flow_Step entity = m_Rep.GetById(id);
                Flow_StepModel model = new Flow_StepModel();
                model.Id = entity.Id;
                model.Name = entity.Name;
                model.Remark = entity.Remark;
                model.Sort = entity.Sort;
                model.FormId = entity.FormId;
                model.FlowRule = entity.FlowRule;
                model.IsCustom = entity.IsCustom;
                model.IsAllCheck = entity.IsAllCheck;
                model.Execution = entity.Execution;
                model.CompulsoryOver = entity.CompulsoryOver;
                model.IsEditAttr = entity.IsEditAttr;
                if (!string.IsNullOrEmpty(entity.Execution))
                {
                    DBContainer db = new DBContainer();
                    string[] arr = entity.Execution.Split(',');
                    List<string> executionnames = new List<string>();
                    switch (entity.FlowRule)
                    {
                        case 2:
                            executionnames = db.SysUser.Where(a => arr.Contains(a.Id)).Select(a => a.TrueName).ToList();
                            break;
                        case 4:
                            executionnames = db.SysPosition.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                        case 5:
                            executionnames = db.SysStruct.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                    } 
                    model.ExecutionName = string.Join(",", executionnames.ToArray());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        public override List<Flow_StepModel> CreateModelList(ref IQueryable<Flow_Step> queryData)
        {
            List<Flow_StepModel> modelList = (from r in queryData
                                              select new Flow_StepModel
                                              {
                                                  StepNo = "第 " + r.Sort.ToString() + " 步",
                                                  Id = r.Id,
                                                  Name = r.Name,
                                                  Remark = r.Remark,
                                                  Sort = r.Sort,
                                                  FormId = r.FormId,
                                                  FlowRule = r.FlowRule,
                                                  IsCustom = r.IsCustom,
                                                  IsAllCheck = r.IsAllCheck,
                                                  Execution = r.Execution,
                                                  CompulsoryOver = r.CompulsoryOver,
                                                  IsEditAttr = r.IsEditAttr,

                                              }).ToList();

            DBContainer db = new DBContainer();
            List<string> executionnames = new List<string>();
            foreach (var r in modelList)
            {
                if (!string.IsNullOrEmpty(r.Execution))
                {
                    string[] arr = r.Execution.Split(',');
                    switch (r.FlowRule)
                    {
                        case 2:
                            executionnames = db.SysUser.Where(a => arr.Contains(a.Id)).Select(a => a.TrueName).ToList();
                            break;
                        case 4:
                            executionnames = db.SysPosition.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                        case 5:
                            executionnames = db.SysStruct.Where(a => arr.Contains(a.Id)).Select(a => a.Name).ToList();
                            break;
                    }

                    r.Execution = string.Join(",", executionnames.ToArray());
                    r.ExecutionName = r.Execution;
                }
                else
                {

                    r.Execution = r.FlowRule == (int)FlowRuleEnum.Lead ? "按上级"
                        : r.FlowRule == (int)FlowRuleEnum.Person ? "按人员"
                        : r.FlowRule == (int)FlowRuleEnum.Customer ? "待指派"
                        : r.FlowRule == (int)FlowRuleEnum.Position ? "按职位"
                        : r.FlowRule == (int)FlowRuleEnum.Department ? "按部门" : "按规则";
                }

            }
            return modelList;
        }

    }
}
